package com.myway.service.ibe.command;

import com.alibaba.fastjson.JSON;
import com.myway.dto.domain.Cabin;
import com.myway.dto.domain.FlightCriteria;
import com.myway.dto.domain.Segment;
import com.travelsky.ibe.client.*;
import com.travelsky.ibe.exceptions.IBEException;
import com.travelsky.util.QDateTime;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018/1/15.
 * 查询航班
 */
@Slf4j
public class SearchFlightCommand extends BaseIBECommand {


    /**
     * 查询航班
     * @param criteria
     */
    public String execute( FlightCriteria criteria) {
        List<Segment> segmentList = new ArrayList<Segment>();
        //FlightCriteria criteria = (FlightCriteria) params.get("flightCriteria");
        //航班起飞时间
        //String date1 = criteria.getFlightDate();
        //航班是否直达
        boolean direct = criteria.isDirect();
        try {

            Date orgDate = QDateTime.stringToDate(criteria.getFlightDate(), "yyyy-MM-dd");
            //航班起飞地三字码
            String org = criteria.getOrgCity();
            //航班到达地三字码
            String dst = criteria.getDstCity();
            AVReq avReq = new AVReq();
            avReq.setOrg(org);
            avReq.setDst(dst);
            avReq.setDateOfFlight(orgDate);
            avReq.setAirline(criteria.getAirline());
            //avReq.setDirect(direct);
            AV av = new AV();
            this.configIBEClient(av);
            //航班查询结果
            AvResult result = av.getAvailability(avReq);
            if (result != null) {


                for (int i = 0; i < result.getItemamount(); i++) {
                    AvItem ai = result.getItem(i);
                    //copy segment....
                    for (int j = 0; j < ai.getSegmentnumber(); j++) {
                        AvSegment as = ai.getSegment(j);

                        Segment segment = new Segment();
                        segment.setFlightNo(as.getAirline());
                        segment.setPlaneStyle(as.getPlanestyle());

                        segment.setDepartDate(as.getDepdate());
                        segment.setOrgCity(as.getOrgcity());
                        segment.setDstCity(as.getDstcity());
                        segment.setArriveDate(as.getArridate());
                        segment.setStops(as.getStopnumber());
                        segment.setCodeShare(as.isCodeShare());
                        segment.setDepTerm(as.getDepTerm());
                        segment.setArriTerm(as.getArriTerm());
                        //copy cabin
                        for (int k = 0; k < 26; k++) {
                            char cangwei = as.getCangweiCodeSort(k);

                            //log.debug("Cabin name :"+cangwei);
                            if (cangwei == '-') {
                                break;
                            }

                            char cabinInfo = as.getCangweiinfoOfSort(as
                                    .getCangweiCodeSort(k));
                            log.debug("Cabin state :"+cabinInfo);
                            Cabin cabin = new Cabin();
                            cabin.setCabinName(String.valueOf(cangwei));
                            cabin.setQuantity(String.valueOf(cabinInfo));
                            //log.debug(cabin);
                            segment.getCabins().add(cabin);

                            //Search sub Cabins
                            String[] subCabins = as.getSubClassList(cangwei);
                            //found sub cabins
                            if (subCabins != null) {
                                for (String subCabin : subCabins) {
                                    String classInfo = as.getSubClassInfoOf(cangwei, subCabin);
                                    Cabin cabinSub = new Cabin();
                                    cabinSub.setCabinName(subCabin);
                                    cabinSub.setQuantity(classInfo);
                                    segment.getCabins().add(cabinSub);
                                }
                            }
                        }
                        segmentList.add(segment);
                    }
                }
            }
            String json = JSON.toJSONString(segmentList);
            return json;
        } catch (IBEException e) {
            e.printStackTrace();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return "";
    }
}
